import re

# 分组 () search函数可以使用group进行分组获取
text = "John Doe 28"
pattern = "([A-Za-z]+) ([A-Za-z]+) ([0-9]+)"
res = re.search(pattern,text)
print(res)
print(res.group(1))
print(res.group(2))
print(res.group(3))
# () findall 找到匹配字符串所有匹配内容
text = "name:Jack age:20,name:Rose age:15,name:Lucy age:22,"
pattern = r":([A-Za-z]+) [A-Za-z]+:([0-9]+),"
res = re.findall(pattern,text)
print(res)
# 替换 满足规则就进行替换
text = "hello abc,hello adc,hello afc"
pattern = r"a.c"
res = re.sub(pattern,"KFC",text)
print(res)
# text.replace("abc","KFC")
# 切分 按照正则格式切分
text = "hello;world,python scrapy"
pattern = r"[;, ]"
res = re.split(pattern,text)
print(res)
text = "name:Jack age:20,name:Rose age:15,name:Lucy age:22,"
pattern = r"(\w+) (\w+):(\d+),"
res = re.findall(pattern,text)
print(res)
# 标识位的使用
# I 忽略大小写
text = "Hello hello hELLo"
pattern = r"hello"
res = re.findall(pattern,text,re.I)
print(res)
pattern = r"[hH][eE][Ll][Ll][oO]"
res = re.findall(pattern,text)
print(res)
# M 可以分行匹配
text = "152000100025\n15300020003\n15700030004"
pattern = r"^\d{11}$"
res = re.findall(pattern,text,re.M)
print(res)
# 可以将.符号匹配所有字符
text = "abc,adc,afc,a\nc"
pattern = r"a.c"
res = re.findall(pattern,text,re.S)
print(res)
